package net.learn.vocabulary;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.AssetManager;

public class Lesson {
	public ArrayList<String> sWord;
	public ArrayList<String> sMean;
	public ArrayList<String> sGram;
	public ArrayList<String> sConv;
	private Context context;
	public Lesson (String s, Context mContext){
		context = mContext;
		sWord = new ArrayList<String>();
		sMean = new ArrayList<String>();
		sGram = new ArrayList<String>();
		sConv = new ArrayList<String>();
		Load(s);
	}
	private void Load(String s){
		AssetManager am = context.getAssets();
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		try {
			InputStream is = am.open(s);
			int i=0,j=0,iType=-99;
		    while (i != -1){
		    	i = is.read();
		    	if(i == 59){
		    		if(byteArrayOutputStream.toString().equalsIgnoreCase("<word>")){
		    			iType = 0;
		    			byteArrayOutputStream.reset();
			    		continue;
		    		}else if (byteArrayOutputStream.toString().equalsIgnoreCase("\r\n<gram>")){
		    			iType = 1;
		    			byteArrayOutputStream.reset();
			    		continue;
		    		}else if (byteArrayOutputStream.toString().equalsIgnoreCase("\r\n<conv>")){
		    			iType = 2;
		    			byteArrayOutputStream.reset();
			    		continue;
		    		}
		    			    		
		    		if(iType == 0){//Read word data
			    		if((j % 2) == 0)sWord.add(byteArrayOutputStream.toString("Shift_JIS"));
			    		if((j % 2) == 1)sMean.add(byteArrayOutputStream.toString("Shift_JIS"));
			    		j=j+1;
			    		byteArrayOutputStream.reset();
			    		continue;
		    		}else if(iType == 1){//Read grammar data
		    			sGram.add(byteArrayOutputStream.toString("Shift_JIS"));
		    			byteArrayOutputStream.reset();
			    		continue;
		    		}else if(iType == 2){//Read grammar data
		    			sConv.add(byteArrayOutputStream.toString("Shift_JIS"));
		    			byteArrayOutputStream.reset();
			    		continue;
		    		}
		    	}
		    	byteArrayOutputStream.write(i);
		     }
		     is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public int Length(){
		return sWord.size();
	}
	public String Word(int index){
		return sWord.get(index);
	}
	public String Mean(int index){
		return sMean.get(index);
	}
	public String Gram(int index){
		return sGram.get(index);
	}
	public String Conv(int index){
		return sConv.get(index);
	}
	
	                              
}
